package com.awspaass.user.apps.restaurant;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.controller.MsgNoticeController;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
/**
 * 
 * @author zhangchunkui
 * 产品预订确认接单给用户发消息
 */
public class ProductReservationSendMsgAfterLeave  extends ExecuteListener implements ExecuteListenerInterface {
	@Override
	public String getDescription() {
        return "产品预订确认接单给用户发消息！";
    }
	
	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(pec.getTaskInstance(), "预订确认");
		if(flag_ok) {
			try {
				ProcessInstance processInstance = pec.getProcessInstance();
				String bindId = processInstance.getId();//流程实例ID
				String target = pec.getTaskInstance().getTarget();//节点任务办理人
				String guanliyuan =pec.getUserContext().getUserModel().getUserName();//办理人名称
				String telephone =pec.getUserContext().getUserModel().getMobile();//节点办理人电话
				String orderSql = "select id,applyuid,orderdate,area,getfoodtime,period from BO_EU_SHSYST_FOODORDER_HEAD where bindid='"+bindId+"' ";
				List<Map<String, Object>> dataList = DBSql.query(orderSql, new ColumnMapRowMapper(), new Object[] {});
				if (dataList == null || dataList.isEmpty()) {
					return ;
				}
				Map<String, Object> dataMap = dataList.get(0);
				String applyUid = CoreUtil.objToStr(dataMap.get("applyuid"));//预订人id
				String orderdate = CoreUtil.objToStr(dataMap.get("orderdate"));//预订日期
				String area = CoreUtil.objToStr(dataMap.get("area"));//取货食堂
				String getfoodtime = CoreUtil.objToStr(dataMap.get("getfoodtime"));//取货时间
				String period = CoreUtil.objToStr(dataMap.get("period"));//取菜时间段 0:午餐时段|1:晚餐时段
				Date date = new SimpleDateFormat("yyyy-MM-dd").parse(getfoodtime);
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
//				Integer year=cal.get(Calendar.YEAR);//获取年
				Integer month = cal.get(Calendar.MONTH)+1;//获取月（月份从0开始，如果按照中国的习惯，需要加一）
				Integer day_moneth=cal.get(Calendar.DAY_OF_MONTH);//获取日（月中的某一天）
				String quhuo = month.toString()+"月"+day_moneth+"日";
				String ycst = null;
				if("0".equals(period)) {//0:午餐|1:晚餐|2:早餐|3:夜宵
					 ycst ="午餐时段";
				}else if("1".equals(period)) {
					 ycst ="晚餐时段";
				}
				String areaName = CoreUtil.objToStr(DBSql.getString("select item.cnname areaname from Bo_Act_Dict_Kv_Item item "
						+ "left join BO_ACT_DICT_KV_MAIN main on item.bindid = main.bindid "
						+ "where substr('"+area+"',0,2)=item.itemno and main.dictkey = 'shdininghallarea'", "areaname"));
				
				String applyName = SDK.getORGAPI().getUser(applyUid).getUserName();
				String applyMobile = SDK.getORGAPI().getUser(applyUid).getMobile();
				//发消息
				String msg = "尊敬的"+applyName+"，您已成功预订"+getfoodtime+areaName+"的外卖食品，领取时间为"+quhuo+ycst+"，如有任何疑问请及时联系："+guanliyuan+"，电话："+telephone+"，恭候您的光临。";
				MsgNoticeController.sendNoticeMsg(UserContext.fromUID(target), msg,"admin", applyUid, "1", "");
				//发短信
				
				String param = "{'CONTACTPERSON':'"+applyName+"','YCDATEAREA':'"+getfoodtime+areaName+"','COLLECTIONTIME':'"+quhuo+ycst+
						"','GUANLIYUAN':'"+guanliyuan+"','STPHONE':'"+telephone+"'}";
				SmsUtil.sendSms(applyMobile,"SMS_256000118",param);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}
